package cn.opopopo.design_pattern_learn.结构型.flyweight;

public class FlyweightPatternDemo {
    public static void main(String[] args) {
        // 模拟创建大量字符
        Character[] characters = new Character[10]; // 简化为 10 个

        // 记录起始样式数量
        System.out.println("初始样式数量: " + CharacterStyleFactory.getStyleCount());

        // 创建字符，但样式尽量复用
        CharacterStyle style1 = CharacterStyleFactory.getStyle("宋体", 12, "黑色");
        CharacterStyle style2 = CharacterStyleFactory.getStyle("黑体", 14, "红色");
        CharacterStyle style3 = CharacterStyleFactory.getStyle("宋体", 12, "黑色"); // 与 style1 相同

        characters[0] = new Character('H', style1);
        characters[1] = new Character('e', style1);
        characters[2] = new Character('l', style1);
        characters[3] = new Character('l', style1);
        characters[4] = new Character('o', style1);
        characters[5] = new Character('!', style2);
        characters[6] = new Character('W', style1); // 复用 style1
        characters[7] = new Character('o', style1);
        characters[8] = new Character('r', style1);
        characters[9] = new Character('l', style1);

        // 显示所有字符（传入外部状态：位置）
        for (int i = 0; i < characters.length; i++) {
            characters[i].display(i * 10, 0); // 模拟 x 坐标
        }

        // 查看共享效果
        System.out.println("最终样式对象数量: " + CharacterStyleFactory.getStyleCount());
        System.out.println("共创建了 " + characters.length + " 个字符对象，但只用了 " +
                CharacterStyleFactory.getStyleCount() + " 个样式对象（享元）");
    }
}